package com.os4j.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class TestAbstract {

	public static void main(String[] args) {
		testFastjsonCompartTime();
		testFastjsonCompart();
		testFastjsonCompartArray();
	}
	public static void testFastjsonCompartTime(){
		String str1 = "{"
					+ "\"customerId\":1039,"
					+ "\"id\":1028,"
					+ "\"creditLine\":"
						+ "["
							+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
							+ "\"attributeList\":"
								+ "["
									+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
								+ "],"
							+ "}"
						+ "],"
					+ "\"materialList\":"
						+ "["
							+ "{\"materialId\":10,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\","
							+ "}"
						+ "]"
				+ "}"; 
		
		String str2 = "{"
				+ "\"customerId\":1039,"
				+ "\"id\":1028,"
				+ "\"creditLine\":"
					+ "["
						+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
						+ "\"attributeList\":"
							+ "["
								+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
							+ "],"
						+ "}"
					+ "],"
				+ "\"materialList\":"
					+ "["
						+ "{\"materialId\":10,\"type\":\"BASIC2\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"}"
					+ "]"
			+ "}"; 
	
		JSONObject json1 = JSON.parseObject(str1);
		JSONObject json2 = JSON.parseObject(str2);
		
		long start = System.currentTimeMillis();
		for(int i=0;i<100000;i++){
			Map<String,Object> map = new HashMap<String,Object>();
			Map<String,Object> tmap = new FastjsonArrayUtils().compartr(map, json1, json2);
		}
		long end = System.currentTimeMillis();
		
		long time = (end - start)/1000;
		System.out.println("用时:"+time+"s");
		
        
	}
	public static void testFastjsonCompart(){
		String str1 = "{"
					+ "\"customerId\":1039,"
					+ "\"id\":1028,"
					+ "\"creditLine\":"
						+ "["
							+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
							+ "\"attributeList\":"
								+ "["
									+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
								+ "],"
							+ "}"
						+ "],"
					+ "\"materialList\":"
						+ "["
							+ "{\"materialId\":10,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\","
							+ "}"
						+ "]"
				+ "}"; 
		String str2 = "{"
				+ "\"customerId\":1039,"
				+ "\"id\":1028,"
				+ "\"creditLine\":"
					+ "["
						+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
						+ "\"attributeList\":"
							+ "["
								+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
							+ "],"
						+ "}"
					+ "],"
				+ "\"materialList\":"
					+ "["
						+ "{\"materialId\":10,\"type\":\"BASIC2\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"}"
					+ "]"
			+ "}"; 
	
		JSONObject json1 = JSON.parseObject(str1);
		JSONObject json2 = JSON.parseObject(str2);
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> tmap = new FastjsonArrayUtils().compartr(map, json1, json2);
		System.out.println(tmap);
		JSONObject jObj = JSONObject.parseObject(JSON.toJSONString(tmap));
		System.out.println(jObj);
	}
	public static void testFastjsonCompartArray(){
		String str1 = 
		"{"
			+ "\"userId\":1002,"
			+ "\"applicationBill\":{"
					+ "\"customerId\":1039,"
					+ "\"id\":1028,"
					+ "\"creditLine\":"
						+ "["
							+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
							+ "\"attributeList\":"
								+ "["
									+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
								+ "],"
							+ "}"
						+ "],"
					+ "\"materialList\":"
						+ "["
							+ "{\"materialId\":10,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
							+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\","
							+ "}"
						+ "]"
			+ "}"
		+ "}";
		String str2 = 
		"{"
			+ "\"userId\":1003,"
			+ "\"applicationBill\":{"
				+ "\"customerId\":1039,"
				+ "\"id\":1028,"
				+ "\"creditLine\":"
					+ "["
						+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"id\":13,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
						+ "\"attributeList\":"
							+ "["
								+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
							+ "],"
						+ "}"
					+ "],"
				+ "\"materialList\":"
					+ "["
						+ "{\"materialId\":10,\"type\":\"BASIC2\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"materialId\":13,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"}"
					+ "]"
			+ "}"
		+ "}";
		String str3 = 
		"{"
			+ "\"userId\":1005,"
			+ "\"applicationBill\":{"
				+ "\"customerId\":1089,"
				+ "\"id\":1028,"
				+ "\"creditLine\":"
					+ "["
						+ "{\"id\":10,\"appliedAmount\":1206,\"bizType\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"id\":23,\"appliedAmount\":1204,\"bizType\":\"FACTORING_OPEN_RECOURSE\","
						+ "\"attributeList\":"
							+ "["
								+ "{\"attributeDefinitionId\":50,\"cnName\":\"BASIC\",\"fieldType\":\"BASIC\"},"
							+ "],"
						+ "}"
					+ "],"
				+ "\"materialList\":"
					+ "["
						+ "{\"materialId\":10,\"type\":\"BASIC2\",\"name\":\"FACTORING_OPEN_RECOURSE\"},"
						+ "{\"materialId\":23,\"type\":\"BASIC\",\"name\":\"FACTORING_OPEN_RECOURSE\"}"
					+ "]"
			+ "}"
		+ "}";
		List<Object> list = new ArrayList<Object>();
		FastjsonUtils fast = new FastjsonArrayUtils();
		list.add(fast.str2JSONObject(str1));list.add(fast.str2JSONObject(str2));list.add(fast.str2JSONObject(str3));
		Map<String,Object> modp = new TreeMap<String,Object>();
		for(int i=0;i<list.size()-1;i++){
			JSONObject json1 = (JSONObject) list.get(i);
			JSONObject json2 = (JSONObject) list.get(i+1);
			Object userId = json2.get("userId");
			JSONObject app1 = (JSONObject) json1.get("applicationBill");
			JSONObject app2 = (JSONObject) json2.get("applicationBill");
			Map<String,Object> map = new HashMap<String,Object>();
			Map<String,Object> tmap = fast.compartr(map, app1, app2);
			modp.put(userId.toString(), tmap);
		}
		System.out.println(JSON.toJSONString(modp));
	}
}
